package com.honeycomb.tag.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honeycomb.tag.dto.TagInfoDto;
import com.honeycomb.tag.service.TagService;
import com.honeycomb.user.dto.UserinfoDto;
import com.honeycomb.utils.ErrorMsgConstants;
import com.honeycomb.utils.SystemConstants;

/**
 * 标签控制器
 * 
 * @author Wangyu
 */
@Controller
@RequestMapping("tag/")
public class TagControl {

	/**
	 * 
	 *
	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private TagService tagService;


	@RequestMapping("ajaxSelectFavorTagList")
	@ResponseBody
	public Map<String, Object> ajaxSelectFavorTagList(HttpServletRequest request)
			throws Exception {
		logger.info("-----------tag---------ajaxSelectFavorTagList--------------------");

		Map<String, Object> mapObj = new HashMap<String, Object>();
			UserinfoDto user = (UserinfoDto) request.getSession().getAttribute(
					SystemConstants.LOGINUSER_SESSION_NAME);
			List<TagInfoDto> tagInfoDtoList = tagService.getTagsByUser(String.valueOf(user.getUserId()));
			mapObj.put("result", SystemConstants.AJAX_SUCCESS);
			List<Map<String, Object>> tagList = new ArrayList<Map<String, Object>>();
			for (TagInfoDto tagElem : tagInfoDtoList) {
				Map<String, Object> tagData = new HashMap<String, Object>();
				tagData.put("tagId", tagElem.getTagId());
				tagData.put("parentId", tagElem.getParentId());
				tagData.put("tagName", tagElem.getTagName());
				tagData.put("tagLogoUrl", tagElem.getTagLogoUrl());

				tagList.add(tagData);
			}
			mapObj.put("data", tagList);

		return mapObj;
	}


	@RequestMapping("ajaxDeleteFavorTag")
	@ResponseBody
	public boolean ajaxDeleteFavorTag(HttpServletRequest request)
			throws Exception {
		logger.info("-----------tag---------ajaxDeleteFavorTag--------------------");
		// 未实现，并不是删除标签，而是删除标签和该用户的关联关系。需要同时传入tagId和userId
		boolean flag = false;
		String tagId = request.getParameter("tagId");
		if (tagId != null && !tagId.equals("")){
			flag = tagService.deleteFavorTag(tagId);
		} else {
			return false;
		}
		
		return flag;
		// return "sorry, System busy!!!";
	}


	@RequestMapping("ajaxSelectOneTypeTags")
	@ResponseBody
	public Map<String, Object> ajaxSelectOneTypeTags(HttpServletRequest request) {
		logger.info("-----------tag---------ajaxSelectOneTypeTags--------------------");
		String tagId = request.getParameter("tagId").trim();
		logger.info("info tagTypeId=" +tagId);
		Map<String, Object> mapObj = new HashMap<String, Object>();
			List<TagInfoDto> tagInfoDtoList = tagService.getChildrenTags(tagId);
			mapObj.put("result", SystemConstants.AJAX_SUCCESS);
			List<Map<String, Object>> tagList = new ArrayList<Map<String, Object>>();
			for (TagInfoDto tagElem : tagInfoDtoList) {
				Map<String, Object> tagData = new HashMap<String, Object>();
				tagData.put("tagId", tagElem.getTagId());
				tagData.put("parentId", tagElem.getParentId());
				tagData.put("tagName", tagElem.getTagName());
				tagData.put("tagLogoUrl", tagElem.getTagLogoUrl());

				tagList.add(tagData);
			}
			mapObj.put("data", tagList);

		return mapObj;
	}

	@RequestMapping("ajaxAttentionTag")
	@ResponseBody
	public boolean ajaxAttentionTag(HttpServletRequest request)
			throws Exception {
		logger.info("-----------tag---------ajaxAttentionTag--------------------");
		boolean flag = false;
		// 未实现，需要在数据库中添加一条该标签和该用户的关联关系。需要同时传入tagId和userId
		UserinfoDto user = (UserinfoDto) request.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
		String userId = String.valueOf(user.getUserId());
		String tagId = request.getParameter("tagId");
		if (tagId != null && !tagId.equals("")){
			flag = tagService.attentionTag(userId, tagId);
		} else {
			return false;
		}

		return flag;
	}

	@RequestMapping("ajaxSelectTagType")
	@ResponseBody
	public Map<String, Object> ajaxSelectTagType() {
		logger.info("-----------tag---------ajaxSelectTagType--------------------");
		Map<String, Object> mapObj = new HashMap<String, Object>();

			List<TagInfoDto> tagList = tagService.getRootTags();
			mapObj.put("result", SystemConstants.AJAX_SUCCESS);
			TagInfoDto elem = null;
			Iterator<TagInfoDto> iterator = tagList.iterator();
			List<Map<String, Object>> tageTypeMapList = new ArrayList<Map<String,Object>>();
			for (int i = 0; iterator.hasNext(); i++) {
				Map<String, Object> tageTypeMap = new HashMap<String, Object>();
				elem = iterator.next();
				tageTypeMap.put("tagId", elem.getTagId());
				tageTypeMap.put("tagName", elem.getTagName());
				
				tageTypeMapList.add(tageTypeMap);
			}

			mapObj.put("tagList",tageTypeMapList);

		return mapObj;
	}
*/
	
}
